#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <atomic>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    class TimeUtil
    {
    public:
        static std::string GetTimeStamp()
        {
            struct timeval tv;
            gettimeofday(&tv, nullptr);
            return std::to_string(tv.tv_sec);
        }

        static std::string GetTimeMs()
        {
            struct timeval tv;
            gettimeofday(&tv, nullptr);
            return std::to_string(tv.tv_sec * 1000 /*秒值*/+ tv.tv_usec / 1000/*微秒值*/);
        }
    };

    const std::string affix = "./temp"; // 前缀
    class PathUtil
    {
    public:
        // 输入值：文件名称 eg.xxx

        static std::string AddSuffix(const std::string& file_name, const std::string& suffix) // 添加后缀
        {
            std::string ret = affix;
            ret += "/";
            ret += file_name;
            ret += suffix;
            return ret;
        }

        // 代码文件："./temp/xxx.cpp"
        static std::string Src(const std::string& file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }

        // 可执行文件："./temp/xxx.exe"
        static std::string Exe(const std::string& file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        
        // 编译错误文件："./temp/xxx.CompileError"
        static std::string CompileError(const std::string& file_name)
        {
            return AddSuffix(file_name, ".CompileError");
        }

        // 默认输入流文件："./temp/xxx.stdin"
        static std::string Stdin(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }

        // 默认输出流文件："./temp/xxx.error"
        static std::string Stdout(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }

        // 默认错误流文件："./temp/xxx.error"
        static std::string Stderr(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

    class FileUtil
    {
    public:
        static bool isFileExist(const std::string& path_name)
        {
            struct stat st;
            if(stat(path_name.c_str(), &st) == 0)
            {
                return true;
            }
            return false;
    
        }
        // 毫秒级时间戳 + 原子性唯一增长主值
        static std::string GetUniqeName()
        {
            static std::atomic<uint64_t> id(0);
            id++;
            return TimeUtil::GetTimeMs() + std::to_string(id);
        }

        static bool WriteFile(const std::string& src, const std::string& dst)
        {
            std::ofstream ofs(dst);
            if(!ofs.is_open())
            {
                return false;
            }

            ofs.write(src.c_str(), src.size());
            ofs.close();
            return true;
        }

        static bool ReadFile(const std::string& src_file, std::string* out, bool isLineBreak = false /*是否保留换行*/)
        {
            std::ifstream ifs(src_file);
            if(!ifs.is_open())
            {
                return false;
            }

            std::string line; // getline函数不会保留换行符，所以要保留换行符需要自己选择
            while(std::getline(ifs, line))
            {
                (*out) += line + (isLineBreak ? "\n" : "");
            }
            ifs.close();
            return true;
        }
    };

    class ResourceUtil
    {
    public:
        // cpu_limit -- s , mem_limit -- kb
        static void SetResourceLimit(int cpu_limit, int mem_limit)
        {
            struct rlimit rcpu;
            rcpu.rlim_max = RLIM_INFINITY;
            rcpu.rlim_cur = cpu_limit;
            setrlimit(RLIMIT_CPU, &rcpu);

            struct rlimit rmem;
            rmem.rlim_max = RLIM_INFINITY;
            rmem.rlim_cur = mem_limit * 1024; // 注意，给的内存太小的话会出现段错误，连程序都打不开
            setrlimit(RLIMIT_AS, &rmem);
        }
    };

    class StringUtil
    {
    public:
        static void SpiltString(const std::string& raw, std::vector<std::string>* ret, std::string sep)
        {
            boost::split(*ret, raw, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}